/**
 *
 *   Copyright (C) 2011  Fernando Gil
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provieded with
 *   this file.
 *      1. Preserve the original authors’ names.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 *
 *   @author  Fernando Gil
 *   @mail    lobho.gil@gmail.com
 *   @company Development Solutions (DS) Software
 *   @date    18-10-2011
 *   @modify  18-10-2011
 *   @by      Fernando Gil
 *   @see     License GNU GPL v3  (/lic/GNU_GPLv3_EN)
 *   @see     Additional Terms    Point 7.1 on License (/lic/GNU_GPLv3_EN)
 *   @note    Replicate this header in all classes.
 *
 */
package org.dss.gil.util;

import javax.swing.table.DefaultTableModel;


 public class SimTableModel extends DefaultTableModel{

        //Types. Boolean, String, String, String, Sring
        Class[] types = new Class [] {
            java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };

        public SimTableModel(Object newRow[][], String[] columNames) {
            super(newRow,columNames);
        }


        public SimTableModel(){}
        

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        /** Return the row's content with the null values as "" */
        public Object [] getRow(int index){
            Object[] row = {
                this.getValueAt(index, Globals.ACTIVATION_COL),
                this.getValueAt(index, Globals.LABEL_COL),
                this.getValueAt(index, Globals.BLOCK_COL),
                this.getValueAt(index, Globals.PARAMS_COL),
                this.getValueAt(index, Globals.COMMENTS_COL)
            };
            for(int i=0;i<row.length;i++){
                if(row[i] == null)
                    row[i] = "";
            }
            return row;
        }

        /** Return the row's content as String*/
        public String [] getStringRow(int index){
            Object[] row = this.getRow(index);
            String rowString [] = new String [5] ;
            for(int i=0;i<row.length;i++){
                rowString[i] = row[i].toString().trim();
            }
            return rowString;
        }
        
        /** Set the values in a existent row
         * @param index
         * @param values
         */
        public void setRowValues(int index, Object[] values){

            this.setValueAt(values[0], index, 0);
            this.setValueAt(values[1], index, 1);
            this.setValueAt(values[2], index, 2);
            this.setValueAt(values[3], index, 3);
            this.setValueAt(values[4], index, 4);
        }
        
        /** Count the number of rows activated (blocks) before the selected row, inclusive*/
        public int getBlockCounter(int actualRow) throws Exception{
            int counter = 0;
            try{
            for(int i=0;i<=actualRow;i++){
                if(Boolean.parseBoolean(this.getValueAt(i, Globals.ACTIVATION_COL).toString()))
                    counter++;
            }
            }catch(ArrayIndexOutOfBoundsException ex){
               // Globals.log(null,ex);
            }
            return counter;
        }
        
        public boolean isActiveRow(int index){
            return (Boolean)getValueAt(index, Globals.ACTIVATION_COL);
        }

 }
